#
import struct
import logging

class UserButton:
    logger = logging.getLogger("rollo.beagle.UserButton")

    def listen(self,callback,indefinite=True):
	self.file = open('/dev/input/event0', "rb")
	event = self.file.read(16)
	while event:
	  (time1, time2, type, code, value) = struct.unpack('iihhi', event)
	  if type == 1 and code == 276 and value == 1:
	  	self.logger.debug("Userbutton pressed!")
    		callback()
		if(indefinite==False):
			self.close()
			break;
		#break
	  event = self.file.read(16)
    def close(self):
	self.file.close()
